package com.carmaintain.servlets.merchant;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.db.MerchantLib;
import com.carmaintain.db.UserLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class ModifyPlace
 */
public class ModifyPlace extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyPlace() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		String userid;
		String placeId = "";
		String placeName = "";
		String linkMan = "";
		String officePhone = "";
		String address = "";
		String roadNearby = "";
		String gpsLocation = "0,0";
		String mobile = "";
		String businessItem = "";
		int type = 0;
		
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_SIGN_ERROR);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			userid = UserLib.checkUserLogin(request.getParameter("merchant_id"), request.getParameter("usercode"), Constants.USER_TYPE_MERCHANT);
			if (userid == null) {
				throw new Exception();
			}			
			if(Utils.empty(request.getParameter("place_id")) || Utils.empty(request.getParameter("place_name")) 
					|| Utils.empty(request.getParameter("place_linkman")) || Utils.empty(request.getParameter("office_phone")) 
					|| Utils.empty(request.getParameter("address"))){
				result.setCode(Constants.CODE_MISS_PARAMATER);
				result.setMessage(Constants.MESSAGE_MISS_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}else{
				placeId = request.getParameter("place_id").trim();
				placeName = request.getParameter("place_name").trim();
				linkMan = request.getParameter("place_linkman").trim();
				officePhone = request.getParameter("office_phone").trim();
				address = request.getParameter("address").trim();
				gpsLocation = Utils.getGpsLocation(address);
			}
			if(!Utils.empty(request.getParameter("road_nearby"))){
				roadNearby = request.getParameter("road_nearby").trim();
			}
			if(!Utils.empty(request.getParameter("mobile"))){
				mobile = request.getParameter("mobile").trim();
			}	
			if(!Utils.empty(request.getParameter("business_item"))){
				businessItem = request.getParameter("business_item").trim();
			}
			if(!Utils.empty(request.getParameter("type"))){
				type = Integer.parseInt(request.getParameter("type").trim());
			} else {
				type = 0;
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		
		try {
			int ret = MerchantLib.modifyPlace(placeId ,userid, placeName, linkMan, officePhone, address, gpsLocation, roadNearby, mobile, businessItem, type);
			if (ret == Constants.CODE_SUCCESS) {
				result.setCode(Constants.CODE_SUCCESS);
				result.setMessage(Constants.MESSAGE_SUCCESS);
			} else {
				result.setCode(Constants.CODE_FAIL);
				result.setMessage(Constants.MESSAGE_FAIL);
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
